<?php

/**
 * AutocompleteModel
 * Model for get value for autocomplete field
 * 
 * @author tuanna
 * @version 1.0
 */


/**
 * @name AutocompleteModel
 * Class contant static method provides value for autocomplete fields
 * 
 * @author tuanna
 * @version 1.0
 * @date 23/05/2011
 */
class AutocompleteModel{
	/**
	 * @name getChannel
	 * @todo get all server for auto complete controller
	 * @param term
	 * @return array
	 */
	public static function getServer($term){
		Zend_Loader::loadClass("ItemModel");
		
		$db = Zend_Registry::get('connectDB');
		$select = $db->select()
					->distinct()
					->from(array('i' => 'item'), 'server')
					->where("server <> ''")
					->where("server like '%$term%'")
					->limit(10);
		$list = $db->fetchAll($select);
		
        foreach ($list as $row){
            $temp["id"] = $row['server'];
            $temp["label"] = '<span>'.$row['server'].'<span>';
            $temp["value"] = $row['server'];
            $arr[] = $temp;
        }
        
        return $arr;
	}
	
	/**
	 * @name getWork
	 * @todo get all work for auto complete controller
	 * @param term
	 * @return array
	 */
	public static function getWork($term){
		Zend_Loader::loadClass("WorkModel");
		
		$db = Zend_Registry::get('connectDB');
		$select = $db->select()
					->distinct()
					->from(array('w' => 'work'), 'name')
					->where("name <> ''")
					->where("name like '%$term%'")
					->where('status = 1')
					->limit(10);
		$list = $db->fetchAll($select);
		
        foreach ($list as $row){
            $temp["id"] = $row['name'];
            $temp["label"] = '<span>'.$row['name'].'<span>';
            $temp["value"] = $row['name'];
            $arr[] = $temp;
        }
        
        return $arr;
	}
	
	/**
	 * @name getType
	 * @todo get all type for auto complete controller
	 * @param term
	 * @return array
	 */
	public static function getItemType($term){
		Zend_Loader::loadClass("ItemModel");
		
		$db = Zend_Registry::get('connectDB');
		$select = $db->select()
					->distinct()
					->from(array('i' => 'item'), 'type')
					->where("type <> ''")
					->where("type like '%$term%'")
					->limit(10);
		$list = $db->fetchAll($select);
		
        foreach ($list as $row){
            $temp["id"] = $row['type'];
            $temp["label"] = '<span>'.$row['type'].'<span>';
            $temp["value"] = $row['type'];
            $arr[] = $temp;
        }
        
        return $arr;
	}
}
